package models;

import java.util.*;
import java.io.File;

import javax.imageio.ImageIO;

import net.solosky.litefetion.*;
import net.solosky.litefetion.bean.*;
import net.solosky.litefetion.notify.*;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;


import org.apache.log4j.Logger;

public class Fetion {
  
  private static Logger logger = Logger.getLogger(Fetion.class);
  
  public static HashMap<String,Fetion> fetions = new LinkedHashMap<String,Fetion>();
  
  private String mobile;
  
  private LiteFetion client;
  
  private VerifyImage loginVerifyImg;
  
  private VerifyImage addBuddyVerifyImg;
  
  public Fetion(String mobile) {
	logger.debug("new Fetion(" + mobile +")");
    this.mobile = mobile;
    this.client = new LiteFetion();
    fetions.put(mobile,this);
  }
  
  public static Fetion findOrNewByMobile(String mobile) {
    if( fetions.get(mobile) == null) {
      new Fetion(mobile);
    }
    return fetions.get(mobile);
  }
  
  public VerifyImage getLoginVerifyImg() {
    if( this.loginVerifyImg == null) {
      this.resetLoginVerifyImg();
    }
    return this.loginVerifyImg;
  }
  
  public void resetLoginVerifyImg() {
    this.loginVerifyImg = client.retireVerifyImage(VerifyImage.TYPE_LOGIN);
  }
  
  public String getLoginVerifyImgBase64() {
    return (new BASE64Encoder()).encode(this.loginVerifyImg.getImageData());
  }
  
  public VerifyImage getAddBuddyVerifyImg() {
    if( this.addBuddyVerifyImg == null) {
      this.resetAddBuddyVerifyImg();
    }
    return this.addBuddyVerifyImg;
  }
  
  public void resetAddBuddyVerifyImg() {
    this.addBuddyVerifyImg = client.retireVerifyImage(VerifyImage.TYPE_ADD_BUDDY);
  }
  
  public String getAddBuddyVerifyImgBase64() {
    return (new BASE64Encoder()).encode(this.addBuddyVerifyImg.getImageData());
  }
  
  public ActionResult login(String password,String verifyCode) {
    this.getLoginVerifyImg().setVerifyCode(verifyCode);
    ActionResult result = client.login(this.mobile,password, Presence.ONLINE, this.loginVerifyImg);
    if(result != ActionResult.SUCCESS) {
      this.resetLoginVerifyImg();
    } else {
	  this.addBuddyVerifyImg = null;
	}
    return result;
  }
  
  public ActionResult logout() {
    ActionResult result = client.logout();
	// 自杀
	Fetion newFetion = new Fetion(this.mobile);
    return result;
  }
  
  public ClientState getClientState() {
    return this.client.getClientState();
  }
  
  public List<Notify> getNotifies() {
    return this.client.pollNotify();
  }
  
  public ActionResult addBuddy(String distMobile, String localName,String verifyCode) {
    this.getAddBuddyVerifyImg().setVerifyCode(verifyCode);
    ActionResult result = this.client.addBuddy(distMobile,"",localName, null , this.addBuddyVerifyImg);
//	System.out.println("AddBuddy:"+client.addBuddy("13887654321`", "小牛", "峰子", null ,vc ));
    if(result != ActionResult.SUCCESS) {
      this.resetAddBuddyVerifyImg();
    } else {      
	  this.addBuddyVerifyImg = null;
	}
    return result;
  }

  public List<Buddy> getBuddyList() {
	return this.client.getBuddyList();
  }

  public ActionResult sendMessage(String distMobile,String fetionUserId,String content) {
    Buddy buddy = getBuddyByMobileOrFetionUserId(distMobile,fetionUserId);
    if(buddy != null) {
	  // 对方不在飞信就手机短信提醒
      ActionResult result = this.client.sendMessage(buddy,content,false);
      return result;
    } else {
      return ActionResult.USER_INVALID;
    }
  }
  
  public Buddy getBuddyByMobileOrFetionUserId(String mobile,String fetionUserId) {
    Iterator<Buddy> it = this.client.getBuddyList().iterator();
		while(it.hasNext()) {
			Buddy buddy = it.next();
			if((Long.parseLong(mobile) == buddy.getMobile() && buddy.getMobile() != 0) 
            || (Integer.parseInt(fetionUserId) == buddy.getUserId() && buddy.getUserId() != 0)) {
				return buddy;
			}
		}
		return null;
  }

  public Buddy getBuddyByMobile(String mobile) {
    return this.getBuddyByMobileOrFetionUserId(mobile,"0");
  }
}